package com.dg.gaming.api.common.util
{

	public class GameProperties extends AbstractProperties
	{
		// Declare some  constants.
		public static const DATE_FORMAT_FULL:String= "dd/MM/yyyy-hh:mm:ss";
		public static const DATE_FORMAT_TIME:String= "hh:mm:ss";

		/** Link to the current version of DG. */
		public static const VERSION:String= "beta 0.3";

		/** Key to the master server. */
		public static const MASTER_SERVE_KEY:String= "master_server";

		/** Key to the administrator. */
		public static const ADMINISTRATOR:Number= 0;
		/** Key to the administrator. */
		public static const ADMINISTRATOR_GAME:String= "admin";
		/** Default port that DG runs on. */
		public static const DEFAULT_PORT:int= 1790;

		/** Default number of players (2). */
		public static const DEFAULT_NUM_OF_PLAYERS:int= 2;

		/** Default minimum number of players (2). */
		public static const DEFAULT_MIN_NUM_OF_PLAYERS:int= 2;

		/** Default maximum number of players (2). */
		public static const DEFAULT_MAX_NUM_OF_PLAYERS:int= 2;

		/** Default required number of players (2). */
		public static const DEFAULT_REQ_NUM_OF_PLAYERS:int= 2;

		/** Default maximum number of table (2). */
		public static const DEFAULT_MAX_NUM_OF_TABLES:int= 50;

		/** Default number of tables per user */
		public static const DEFAULT_MAX_NUM_OF_TABLES_PER_USER:int= 2;

		/** Default ELO starting rating for new users. */
		public static const DEFAULT_ELO_START_RATING:int= 1200;

		/** Default ELO k factor. */
		public static const DEFAULT_ELO_K_FACTOR:Number= 24.0;

		/** Number of games a user must complete not to be provisional. */
		public static const PROVISIONAL_COUNT:int= 10;

		/** Default value if an admin receives all client messages". */
		public static const ADMIN_RECEIVE_MESSAGES:Boolean= true;

		/**
		 * Default maximum number of users which is multiplied by the number
		 * of games running.
		 */
		public static const DEFAULT_MAX_NUM_OF_USERS:int= 100;

		/** Applet parameter "username". */
		public static const APPLET_PARAM_USERNAME:String= "username";

		/** Applet parameter "password". */
		public static const APPLET_PARAM_PASSWORD:String= "password";

		/** Applet parameter "serverhost". */
		public static const APPLET_PARAM_SERVER_HOST:String= "serverhost";

		/** Applet parameter "serverport". */
		public static const APPLET_PARAM_SERVER_PORT:String= "serverport";

		/** Applet parameter "language". */
		public static const APPLET_PARAM_LOCALE:String = "language";
		
		

		// Special DG images (3 in total every game must have).
		public static const IMAGE_DG_TITLE:String= "image.dg.title";
		public static const IMAGE_GAME_TITLE:String= "image.game.title";
		public static const IMAGE_GAME_ICON:String= "image.game.icon";

		// name of the lookandfeel properties file
		private static const DEFAULT_FILENAME:String= "labels";

		// Declare some keys
		private static const KEY_PLAYER_COLOUR:String= "player.colour.";
		private static const KEY_GAME_ID:String= "game.id";
		private static const KEY_MAX_NUM_OF_TABLES:String= "max.num.of.tables";
		private static const KEY_BACKGROUND_COLOUR:String= "background.colour";
		private static const KEY_TITLE_COLOUR:String= "title.colour";
		private static const IMAGE_LABEL:String= "image.";
		private static const KEY_MAX_NUM_OF_TABLES_PER_USER:String=
				"tables.per.user";
		private static const KEY_RULES_FILENAME:String= "rules.file";
		private static const KEY_TABLE_PLAYER_HIGHLIGHT_COLOR:String=
				"table.current.player.highlight.colour";
		private static const KEY_TABLE_PLAYER_TEXT_COLOR:String=
				"table.current.player.text.colour";

		// Some defaults
		private static const DEFAULT_BACKGROUND_COLOUR:String= "212,208,200";
		private static const DEFAULT_TITLE_COLOUR:String= "100,100,100";

		private static var instance:GameProperties= null;

		/**
		 * Private constructor (Can only be called by the getInstance() method.
		 */
		public function GameProperties() {
			super(DEFAULT_FILENAME);
		}

		/**
		 * Accessor to singleton instance of this class.
		 *
		 * @return  Instance of this class.
		 */
		public static function getInstance():GameProperties {
			if (instance == null) {
				instance = new GameProperties();
			}

			return instance;
		}

		/**
		 * Return the minimum number of players in a game.
		 *
		 * @return    Max num of players.
		 */
		public static function getMaxNumOfTables():int {
			return getInstance().getInt(KEY_MAX_NUM_OF_TABLES,
										DEFAULT_MAX_NUM_OF_TABLES);
		}

		/**
		 * Return the maximum number of tables that a user can create.
		 *
		 * @return
		 */
		public static function getMaxNumOfTablesPerUser():int {
			return getInstance().getInt(KEY_MAX_NUM_OF_TABLES_PER_USER,
										DEFAULT_MAX_NUM_OF_TABLES_PER_USER);
		}

		/**
		 * Return the game ID.
		 *
		 * @return    GameID e.g. chess.
		 */
		public static function getGameID():String {
			return getInstance().getValue(KEY_GAME_ID);
		}

		/**
		 * Return the game label on its own.
		 *
		 * @return    Game label (no id) - e.g. chess.
		 */
		public static function getGameNoID():String {
			var gameId:String= getGameID();
			var index:int= gameId.indexOf('-');
			return gameId.substring(0, index - 1);
		}

		/**
		 * Return the color of a player (each player has a specific colour which
		 * is used throughout the API).
		 *
		 * @param playerNum      Supplied player number
		 * @return              Color
		 */
		public static function getPlayerColour(playerNum:int):Color {
			var key:String= KEY_PLAYER_COLOUR + playerNum;
			var colorStr:String= GameProperties.getInstance().getValue(key);
			return DGUtils.getColour(colorStr);
		}

		/**
		 * Return the background colour of the DG table frame.
		 *
		 * @return              Color
		 */
		public static function getBackgroundColour():Color {
			var colorStr:String= GameProperties.getInstance().getValue(
					KEY_BACKGROUND_COLOUR, DEFAULT_BACKGROUND_COLOUR);
			return DGUtils.getColour(colorStr);
		}

		/**
		 * Return the title colour.
		 *
		 * @return
		 */
		public static function getTitleColour():Color {
			var colorStr:String= GameProperties.getInstance().getValue(KEY_TITLE_COLOUR,
					DEFAULT_TITLE_COLOUR);
			return DGUtils.getColour(colorStr);
		}

		/**
		 * <p>Return a vector of images.</p>
		 *
		 * <p>Each image info will consist of the image and its size
		 * in bytes:</p>
		 *
		 *    e.g. chess_icon
		 *
		 * @return   Vector of image strings.
		 */
	//    public static function getImageProperties():Properties {
	//        var properties:Properties= new Properties();
	//
	//        var keys:List= getInstance().getKeys();
	//        for (var i:int= 0; i < keys.size(); i++) {
	//            var key:String= String(keys.getValue(i));
	//            if (key.startsWith("image.")) {
	//                properties.put(key, getInstance().getValue(key));
	//            }
	//        }
	//
	//        // Add special images, if they aren't already defined in the game.properties file
	//        var gameId:String= getInstance().getGameID();
	//        if (!properties.containsKey(IMAGE_DG_TITLE)) {
	//            properties.put(IMAGE_DG_TITLE, "images/dg_title.gif");
	//        }
	//        if (!properties.containsKey(IMAGE_GAME_TITLE)) {
	//            properties.put(IMAGE_GAME_TITLE, "images/" + gameId + "_title.gif");
	//        }
	//        if (!properties.containsKey(IMAGE_GAME_ICON)) {
	//            properties.put(IMAGE_GAME_ICON, "images/" + gameId + "_icon.gif");
	//        }
	//
	//        return properties;
	//    }

		/**
		 * Return the name of the rules file
		 *
		 * @return   Rules filename
		 */
		public static function getRulesFilename():String {
			return getInstance().getValue(KEY_RULES_FILENAME, String(null));
		}

		/**
		 * Return the color to use to highlight the current player in the table list.
		 *
		 * If there is a table.current.player.highlight.colour in the game.properties
		 * file, then that one is used, otherwise the default color provided is used.
		 *
		 * @param defaultColorString   The color to be used if there isn't one
		 *                             defined in the game.properties file.
		 * @return the color to use to highlight the current player in the table
		 *         list display.
		 */
		public static function getTableCurrPlayerHighlightColor(defaultColorString:String):Color {
			var colorStr:String= GameProperties.getInstance().getValue(
					KEY_TABLE_PLAYER_HIGHLIGHT_COLOR, defaultColorString);
			return DGUtils.getColour(colorStr);
		}

		/**
		 * Return the color to use to display the text of the current player in the
		 * table list.
		 *
		 * If there is a table.current.player.text.colour in the game.properties
		 * file, then that one is used, otherwise the default color provided is used.
		 *
		 * @param defaultColorString   The color to be used if there isn't one
		 *                             defined in the game.properties file.
		 * @return the color to use to display the text of the current player in the
		 *         table list display.
		 */
		public static function getTableCurrPlayerTextColor(defaultColorString:String):Color {
			var colorStr:String= GameProperties.getInstance().getValue(
					KEY_TABLE_PLAYER_TEXT_COLOR, defaultColorString);
			return DGUtils.getColour(colorStr);
		}

	}
}